﻿using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using RpcStore.DAL.Model;
using RpcStore.Model.DB;
using RpcStore.Model.TransmitScheme;
using RpcStore.RemoteModel.TransmitScheme.Model;
using WeDonekRpc.SqlSugar;

namespace RpcStore.DAL.Repository
{
    internal class ServerTransmitSchemeDAL : IServerTransmitSchemeDAL
    {
        private readonly IRepository<ServerTransmitSchemeModel> _BasicDAL;
        public ServerTransmitSchemeDAL (IRepository<ServerTransmitSchemeModel> dal)
        {
            this._BasicDAL = dal;
        }
        public ServerTransmitSchemeModel Get (long id)
        {
            ServerTransmitSchemeModel obj = this._BasicDAL.Get(a => a.Id == id);
            if (obj == null)
            {
                throw new ErrorException("rpc.store.transmit.scheme.null");
            }
            return obj;
        }
        public ServerTransmitSchemeModel[] Query (TransmitSchemeQuery query, IBasicPage paging, out int count)
        {
            paging.InitOrderBy("Id", true);
            return this._BasicDAL.Query(query.ToWhere(), paging, out count);
        }
        public void Adds (TransmitSchemeAdd[] data)
        {
            DateTime now = DateTime.Now;
            ServerTransmitSchemeModel[] adds = data.ConvertAll(c => new ServerTransmitSchemeModel
            {
                AddTime = now,
                Scheme = c.Scheme,
                RpcMerId = c.RpcMerId,
                TransmitType = c.TransmitType,
                VerNum = 1,
                Show = c.Show,
                SystemTypeId = c.SystemTypeId,
                Id = IdentityHelper.CreateId(),
                IsEnable = true,
                IsAuto = c.IsAuto
            });
            this._BasicDAL.Insert(adds);
        }
        public long Add (TransmitSchemeAdd scheme)
        {
            ServerTransmitSchemeModel add = new ServerTransmitSchemeModel
            {
                AddTime = DateTime.Now,
                Scheme = scheme.Scheme,
                RpcMerId = scheme.RpcMerId,
                TransmitType = scheme.TransmitType,
                VerNum = 1,
                Show = scheme.Show,
                SystemTypeId = scheme.SystemTypeId,
                Id = IdentityHelper.CreateId(),
                IsEnable = false,
                IsAuto = scheme.IsAuto
            };
            this._BasicDAL.Insert(add);
            return add.Id;
        }
        public BasicScheme[] Clear (long rpcMerId)
        {
            BasicScheme[] schemes = this._BasicDAL.Gets<BasicScheme>(a => a.RpcMerId == rpcMerId);
            if (schemes.IsNull())
            {
                return schemes;
            }
            long[] ids = schemes.ConvertAll(a => a.Id);
            if (!this._BasicDAL.Delete(a => ids.Contains(a.Id)))
            {
                throw new ErrorException("rpc.store.transmit.scheme.delete.fail");
            }
            return schemes;
        }
        public void Delete (long schemeId)
        {
            if (!this._BasicDAL.Delete(a => a.Id == schemeId))
            {
                throw new ErrorException("rpc.store.transmit.scheme.delete.fail");
            }
        }
        public void SetScheme (long id, TransmitSchemeSetDatum set)
        {
            set.VerNum = Tools.GetRandom();
            if (!this._BasicDAL.Update(set, a => a.Id == id))
            {
                throw new ErrorException("rpc.store.transmit.scheme.set.fail");
            }
        }

        public void CheckIsRepeat (long rpcMerId, TransmitSchemeSet data)
        {
            if (this._BasicDAL.IsExist(c => c.RpcMerId == rpcMerId && c.SystemTypeId == data.SystemTypeId && c.Scheme == data.Scheme && c.TransmitType == data.TransmitType))
            {
                throw new ErrorException("rpc.store.transmit.scheme.repeat");
            }
        }

        public void SetIsEnable (long id, bool isEnable)
        {
            if (!this._BasicDAL.Update(a => new ServerTransmitSchemeModel
            {
                IsEnable = isEnable,
                VerNum = Tools.GetRandom()
            }, a => a.Id == id))
            {
                throw new ErrorException("rpc.store.transmit.scheme.set.fail");
            }
        }

        public void CheckIsRepeatEnable (ServerTransmitSchemeModel scheme)
        {
            if (this._BasicDAL.IsExist(c => c.RpcMerId == scheme.RpcMerId && c.SystemTypeId == scheme.SystemTypeId && c.Scheme == scheme.Scheme && c.IsEnable))
            {
                throw new ErrorException("rpc.store.transmit.scheme.repeat.enable");
            }
        }
    }
}
